package Setting;

import java.awt.List;
import java.util.ArrayList;
import java.io.*;

public class SettingData {
	public static ArrayList<int[]> data;
	public static int current_user;
	static private FileWriter bw;
	static private BufferedReader br;

	public SettingData() throws IOException {
		data = new ArrayList<int[]>();
		/* User 정보를 담은 파일 생성 및 존재시 Exception 처리 */
		try {
			br = new BufferedReader(new FileReader("user.txt"));
			System.out.println("Success File Read!");
			bw = new FileWriter("user.txt", true);
			System.out.println("sucess file Write!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			int[] temp = {100,0};
			bw = new FileWriter("user.txt", true);
			bw.flush();
			bw.append("u1r"+temp[0]+"s"+temp[1]+"\n");
			data.add(temp);
			System.out.println("Success File Create");
		}
		int[] temp = {100,0};
		bw = new FileWriter("user.txt", true);
		bw.flush();
		bw.append("u1r"+temp[0]+"s"+temp[1]+"\n");
		data.add(temp);
		current_user = 1;
		readFile();
	}

	public void readFile() throws IOException {
		br = new BufferedReader(new FileReader("user.txt"));
		int c;
		int flag = 0;
		String temp_string = new String();
		int temp_user = 0;
		int temp_rate = 100;
		int temp_sound = 0;
		String temp_state = new String();
		int[] temp = {100,0};


		while((c=br.read()) != -1) {
			temp_string += (char)c;
			if(flag == 3) {
				temp[1] = c;
				data.add(temp);
				flag = 0;
				temp_string = "";
			}
			else if(flag == 2) {
				flag = 0;
				temp[0] = c;
			}
			else if(flag == 1) {
				flag = 0;
				temp_string = "";
			}

			if(temp_string.equals("r")) {
				temp_string = "";
				flag = 2;
			}
			else if(temp_string.equals("s")) {
				flag = 3;
				temp_string = "";
			}
			else if(temp_string.equals("u")) {
				flag = 1;
				temp_string = "";
			}
			//System.out.println(temp_string);
		}
		
		for(int i = 0; i < data.size(); i++) {
			System.out.println("file test : " + data.get(i)[0]+"  " + data.get(i)[1] + "  " + data.size());
		}
	}

	public static void addUserList() throws IOException {
		int[] temp = {100,0};
		br = new BufferedReader(new FileReader("user.txt"));
		System.out.println("Success File Read!");

		bw.flush();

		data.add(temp);
		bw.append("u"+getUserList()+"r"+temp[0]+"s"+temp[1]+"\n");

		System.out.println("test " + br.readLine() + "\nId : "+ data.size());

		br.close();
	}

	public static int getUserList() throws IOException {
		int ch;
		int count_line = 0;
		while((ch=br.read()) != -1) {
			if(ch == '\n')
				count_line++;
		}
		System.out.println("count_line : "+count_line);
		return count_line;
	}

	public static void set_sound_distance(int user_Number, int input_distance, int input_sound) {
		int[] temp = {100,0}; 
		temp[0] = input_distance;
		temp[1] = input_sound;
		data.set(user_Number, temp);


		// test code
		for(int i = 0; i < data.size(); i++)
			System.out.println(data.get(i)[0]+"  " + data.get(i)[1]);
	}
}
